<template>
	<view class="down-copy">
		<block v-if="show">
			<view class="down-copy-box">
				<u-parse :html="productShowInfo"></u-parse>
			</view>
			<view class="down-imgs">
				<image :src="data" v-for="(data,index) in goods_img" :key="index" mode="aspectFill" @click="preview(data,goods_img)"></image>
			</view>
			<view class="down-copy-btn" @click="queue">批量保存图片，去朋友圈分享</view>
		</block>

		<pt-openSetting ref="openSetting" tips="您已关闭相册访问开关，请打开开关后重新保存"></pt-openSetting>
		
	</view>
</template>

<script>
	import {des_encrypt,des_decrypt} from '@/static/utils/utils.js'
	export default {
		data() {
			return {
				show: false,
				productInfo: '',
				productShowInfo: '',
				goods_img: [],
				successCount: 0,
				failCount: 0
			};
		},
		onLoad(options) {
			// #ifdef MP-WEIXIN
			uni.hideShareMenu()
			// #endif
			let url = ''
			if(options.n98package){
				url = encodeURIComponent('https://master.jiaju01.com/web/shop/#/pages/package/n98-info?group_id='+options.goods_id + '&store_id='+options.store_id+'&active=785&parent_id='+JSON.parse(uni.getStorageSync('userInfo')).user_id)
			}else if(options.housepackage){
				url = encodeURIComponent('https://master.jiaju01.com/web/shop/#/pages/package/house-info?group_id='+options.goods_id + '&store_id='+options.store_id+'&active=785&parent_id='+JSON.parse(uni.getStorageSync('userInfo')).user_id)
			}else{
				console.log(options)
				if(options.isMerchant){
					url = encodeURIComponent('https://master.jiaju01.com/web/shop/#/pages/product-info/product-info?goods_id='+options.goods_id + '&store_id='+options.store_id+'&active=785&parent_id='+JSON.parse(uni.getStorageSync('userInfo')).user_id)+'&isMerchant=true'
				}else{
					url = encodeURIComponent('https://master.jiaju01.com/web/shop/#/pages/product-info/product-info?goods_id='+options.goods_id + '&store_id='+options.store_id+'&active=785&parent_id='+JSON.parse(uni.getStorageSync('userInfo')).user_id)
				}
			}
			if(JSON.stringify(options.goods_img).length >= 9){
				this.goods_img = JSON.parse(options.goods_img).slice(0,9)
			}else{
				this.goods_img = JSON.parse(options.goods_img)
			}
			let path = des_encrypt(url)
			uni.showLoading({
				title: '正在生成分享信息'
			})
			this.$u.api.GoodsUrl({url: path}).then(res => {
				this.show = true
				uni.hideLoading()
				let Link = 'https://master.jiaju01.com/web/link/#/?link=' + path.substring(0,8)
				this.productInfo = '【名称】'+ options.goods_name + '\n【简介】' + options.brief +'\n -------------- \n 优惠价：￥' + options.goods_price + ' \n 抢购链接：' + Link + 'id=' + res.datas
				this.productShowInfo =  '<b>【名称】</b>'+ options.goods_name + '<br /><b>【简介】</b>' + options.brief +'<br /> -------------- <br /> <b>优惠价：</b>￥' + options.goods_price + '<br /> <b>抢购链接：</b>' + Link + 'id=' + res.datas
			}).catch(() => {
				// #ifdef MP-WEIXIN || APP-PLUS
				uni.showToast({
					icon: 'none',
					title: '生成失败，请退出重试!'
				})
				// #endif
				// #ifdef H5
				uni.showToast({
					icon: 'none',
					title: '生成失败，请重试!'
				})
				// #endif
			})
		},
		methods: {
			preview(current,urls){
				uni.previewImage({
					current: current,
					urls: urls
				})
			},
			queue() {
				let promise = Promise.resolve()
				this.goods_img.forEach((url, index) => {
					promise = promise.then(() => {
						return this.downCopy(url,index)
					})
				})
				return promise
			},
			downCopy(url,index){
				return new Promise((resolve, reject) => {
					// #ifdef APP-PLUS || H5
					uni.downloadFile({
						url: url,
						complete: (complete) => {
							if(complete.tempFilePath){
								uni.showLoading({
									title: '正在保存'+(index+1)
								})
								uni.saveImageToPhotosAlbum({
									filePath: complete.tempFilePath,
									success: (res) => {
										resolve(res)
										this.successCount++
										if(this.successCount==this.goods_img.length){
											uni.hideLoading()
											uni.setClipboardData({
												data: this.productInfo,
												success: () => {
													this.successCount = 0
													this.failCount = 0
													uni.showToast({
														icon: 'none',
														title: '文案已复制，图片已保存到相册，快去发朋友圈分享吧~',
														duration: 2000
													})
												}
											})
										}
									},
									fail: (err) => {
										uni.showToast({
											icon: 'none',
											title: '保存失败，您已取消保存图片'
										})
										reject(err)
									}
								})
							}
						}
					})
					// #endif
					// #ifdef MP-WEIXIN
					uni.getSetting({
						withSubscriptions: true,
						success: (scope) => {
							if(!scope.authSetting['scope.writePhotosAlbum']){
								uni.authorize({
									scope: 'scope.writePhotosAlbum',
									success: (authSuccess) => {
										console.log(authSuccess)
										uni.downloadFile({
											url: url,
											complete: (complete) => {
												if(complete.tempFilePath){
													uni.showLoading({
														title: '正在保存'+(index+1)
													})
													uni.saveImageToPhotosAlbum({
														filePath: complete.tempFilePath,
														success: (res) => {
															resolve(res)
															this.successCount++
															if(this.successCount==this.goods_img.length){
																uni.hideLoading()
																uni.setClipboardData({
																	data: this.productInfo,
																	success: () => {
																		this.successCount = 0
																		this.failCount = 0
																		uni.showToast({
																			icon: 'none',
																			title: '文案已复制，图片已保存到相册，快去发朋友圈分享吧~',
																			duration: 2000
																		})
																	}
																})
															}
														},
														fail: (err) => {
															uni.showToast({
																icon: 'none',
																title: '保存失败，您已取消保存图片'
															})
															reject(err)
														}
													})
												}
											}
										})
									},
									fail: () => {
										// uni.showToast({
										// 	icon: 'none',
										// 	title: '您已关闭相册访问开关，请开启后重新保存'
										// })
										this.$refs.openSetting.init()
									}
								})
							}else{
								uni.downloadFile({
									url: url,
									complete: (complete) => {
										if(complete.tempFilePath){
											uni.showLoading({
												title: '正在保存'+(index+1)
											})
											uni.saveImageToPhotosAlbum({
												filePath: complete.tempFilePath,
												success: (res) => {
													resolve(res)
													this.successCount++
													if(this.successCount==this.goods_img.length){
														uni.hideLoading()
														uni.setClipboardData({
															data: this.productInfo,
															success: () => {
																this.successCount = 0
																this.failCount = 0
																uni.showToast({
																	icon: 'none',
																	title: '文案已复制，图片已保存到相册，快去发朋友圈分享吧~',
																	duration: 2000
																})
															}
														})
													}
												},
												fail: (err) => {
													uni.showToast({
														icon: 'none',
														title: '保存失败，您已取消保存图片'
													})
													reject(err)
												}
											})
										}
									}
								})
							}
						}
					})
					// #endif
				})
			}
		}
	}
</script>

<style lang="scss">
	.down-copy{
		padding: 30rpx 0;
		.down-copy-box{
			padding: 30rpx;
			margin: 0 30rpx 30rpx;
			font-size: 24rpx;
			line-height: 50rpx;
			background-color: #EEE;
		}
		textarea{
			width: 630rpx;
			font-size: 24rpx;
			line-height: 50rpx;
			background-color: #EEE;
			padding: 30rpx;
			height: 240rpx;
			/* #ifdef APP-PLUS */
			height: 300rpx;
			/* #endif */
			margin: 0 auto;
		}
		.down-imgs{
			padding: 30rpx;
			display: flex;
			flex-wrap: wrap;
			image{
				width: 210rpx;
				height: 210rpx;
				margin-right: 30rpx;
				margin-bottom: 30rpx;
				border: solid 2rpx #EEE;
				box-sizing: border-box;
				&:nth-child(3n){
					margin-right: 0;
				}
			}
		}
		.down-copy-btn{
			margin: 0 70rpx;
			text-align: center;
			border-radius: 8rpx;
			color: #FFF;
			background-color: #141414;
			height: 90rpx;
			line-height: 90rpx;
		}
	}
</style>
